package com.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.domain.entity.Goods;
import com.common.domain.entity.ResponseResult;
import com.common.mapper.GoodsMapper;
import com.common.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
        implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public Goods getGoodsInfo(Long id) {
        Goods info = getById(id);
        return info;
    }

    @Override
    public ResponseResult allGoodsList(Integer pageNum, Integer pageSize, String goodsName) {
        PageHelper.startPage(pageNum,pageSize);

        List<Goods> list =  goodsMapper.getGoodsInfo(goodsName);

        PageInfo<Goods> pageInfo = new PageInfo<>(list);

        pageInfo.setList(list);

        return ResponseResult.okResult(pageInfo);
    }

    @Override
    public ResponseResult insertGoods(Goods goods) {
        save(goods);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteGoods(String ids) {
        String[] split = ids.split(",");
        removeByIds(Arrays.asList(split));
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateGoods(Goods goods) {
        updateById(goods);
        return ResponseResult.okResult();
    }
}
